package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.SysExpertSelect;
import com.ruoyi.system.service.ISysExpertSelectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysProjectExpertExtract;
import com.ruoyi.system.service.ISysProjectExpertExtractService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 专家抽取记录Controller
 *
 * @author ruoyi
 * @date 2024-08-07
 */
@RestController
@RequestMapping("/system/projectexpertextract")
public class SysProjectExpertExtractController extends BaseController {
    @Autowired
    private ISysProjectExpertExtractService sysProjectExpertExtractService;

    @Autowired
    private ISysExpertSelectService sysExpertSelectService;

    /**
     * 查询专家抽取记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:projectexpertextract:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysProjectExpertExtract sysProjectExpertExtract) {
        startPage();
        System.out.println(sysProjectExpertExtract);
        List<SysProjectExpertExtract> list = sysProjectExpertExtractService.selectSysProjectExpertExtractList(sysProjectExpertExtract);
        return getDataTable(list);
    }

    /**
     * 查询专家抽取记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:projectexpertextract:list')")
    @GetMapping("/lists")
    public AjaxResult lists(SysProjectExpertExtract sysProjectExpertExtract) {
        List<SysProjectExpertExtract> list = sysProjectExpertExtractService.selectSysProjectExpertExtractList(sysProjectExpertExtract);
        return success(list);
    }

    /**
     * 导出专家抽取记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:projectexpertextract:export')")
    @Log(title = "专家抽取记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProjectExpertExtract sysProjectExpertExtract) {
        List<SysProjectExpertExtract> list = sysProjectExpertExtractService.selectSysProjectExpertExtractList(sysProjectExpertExtract);
        ExcelUtil<SysProjectExpertExtract> util = new ExcelUtil<SysProjectExpertExtract>(SysProjectExpertExtract.class);
        util.exportExcel(response, list, "专家抽取记录数据");
    }

    /**
     * 获取专家抽取记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:projectexpertextract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysProjectExpertExtractService.selectSysProjectExpertExtractById(id));
    }

    /**
     * 新增专家抽取记录
     */
    @PreAuthorize("@ss.hasPermi('system:projectexpertextract:add')")
    @Log(title = "专家抽取记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysProjectExpertExtract sysProjectExpertExtract) {
        return toAjax(sysProjectExpertExtractService.insertSysProjectExpertExtract(sysProjectExpertExtract));
    }

    /**
     * 修改专家抽取记录
     */
    @PreAuthorize("@ss.hasPermi('system:projectexpertextract:edit')")
    @Log(title = "专家抽取记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysProjectExpertExtract sysProjectExpertExtract) {
        return toAjax(sysProjectExpertExtractService.updateSysProjectExpertExtract(sysProjectExpertExtract));
    }

    /**
     * 删除专家抽取记录
     */
    @PreAuthorize("@ss.hasPermi('system:projectexpertextract:remove')")
    @Log(title = "专家抽取记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysProjectExpertExtractService.deleteSysProjectExpertExtractByIds(ids));
    }

    @GetMapping(value = "/setstatus/{id}/{status}")
    public AjaxResult setstatus(@PathVariable("id") Long id, @PathVariable("status") String status) {
        SysProjectExpertExtract projectExpertExtract = sysProjectExpertExtractService.selectSysProjectExpertExtractById(id);
        if (projectExpertExtract != null) {
            projectExpertExtract.setStatus(status.toString());
            System.out.println(projectExpertExtract);
            sysProjectExpertExtractService.updateSysProjectExpertExtract(projectExpertExtract);

            // 更新
            SysProjectExpertExtract projectExpertExtractParams = new SysProjectExpertExtract();
            projectExpertExtractParams.setExpertSelectId(projectExpertExtract.getExpertSelectId());
            List<SysProjectExpertExtract> projectExpertExtractList = sysProjectExpertExtractService.selectSysProjectExpertExtractList(projectExpertExtractParams);
            if (projectExpertExtractList.size() > 0) {
                int extractedNum = 0;
                for (SysProjectExpertExtract expertExtract : projectExpertExtractList) {
                    if (Integer.parseInt(expertExtract.getStatus()) > 0) {
                        extractedNum = extractedNum + 1;
                    }
                }
                SysExpertSelect expertSelect = sysExpertSelectService.selectSysExpertSelectById(projectExpertExtract.getExpertSelectId());
                if (expertSelect != null){
                    expertSelect.setExtractedNum(extractedNum);
                    expertSelect.setStatus(1);
                    sysExpertSelectService.updateSysExpertSelect(expertSelect);
                }
            }
        }
        return success(1);
    }

    /**
     * 重置抽取
     * @param expertselectid
     * @return
     */
    @GetMapping(value = "/resetextract/{expertselectid}")
    public AjaxResult resetExtract(@PathVariable("expertselectid") Long expertselectid){
        SysProjectExpertExtract projectExpertExtractParams = new SysProjectExpertExtract();
        projectExpertExtractParams.setExpertSelectId(expertselectid);
        List<SysProjectExpertExtract> projectExpertExtractList = sysProjectExpertExtractService.selectSysProjectExpertExtractList(projectExpertExtractParams);
        if (projectExpertExtractList.size() > 0) {
            for (SysProjectExpertExtract expertExtract : projectExpertExtractList) {
                sysProjectExpertExtractService.deleteSysProjectExpertExtractById(expertExtract.getId());
            }

            // 更新
            SysExpertSelect expertSelect = sysExpertSelectService.selectSysExpertSelectById(expertselectid);
            if (expertSelect != null){
                expertSelect.setExtractedNum(0);
                expertSelect.setStatus(0);
                sysExpertSelectService.updateSysExpertSelect(expertSelect);
            }
        }
        return success(1);
    }

    /**
     * 确认抽取
     * @param expertselectid
     * @return
     */
    @GetMapping(value = "/confirmextract/{expertselectid}")
    public AjaxResult confirmExtract(@PathVariable("expertselectid") Long expertselectid){
        SysExpertSelect expertSelect = sysExpertSelectService.selectSysExpertSelectById(expertselectid);
        if (expertSelect != null){
            expertSelect.setStatus(2);
            sysExpertSelectService.updateSysExpertSelect(expertSelect);
        }
        return success(1);
    }
}
